package top.truehaku.generator.code.domain.model.code;

import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;

@Getter
public class Code {
    private CodeId codeId;
    private CodeDirectory directory;
    private CodeFile file;
    private CodeProperty codeProperty;
    private CodeTemplate codeTemplate;
    @Setter
    private Code parent;
    @Setter
    private List<Code> depends;

    public Code(CodeId codeId,CodeDirectory directory, CodeFile file,CodeProperty codeProperty,CodeTemplate codeTemplate) {
        this.directory = directory;
        this.file = file;
        this.codeProperty = codeProperty;
        this.codeTemplate = codeTemplate;
    }

    public String getPath(){
        return this.directory.getDirectory()+this.file.getFilename();
    }

    public String getTypeName(){
        return this.directory.getPackage()+"."+this.file.getSimpleName();
    }

    /**
     * 该类需要导入的包
     * @return
     */
    public Set<String> getImports(){
        Set<String> imports = this.codeProperty.getImports();
        if (parent != null) {
            if (!this.getDirectory().getPackage().equals(parent.getDirectory().getPackage())){
                imports.add(this.parent.getTypeName());
            }
        }
        if (!CollectionUtils.isEmpty(depends)){
            depends.forEach(item->{
                if (!this.getDirectory().getPackage().equals(item.getDirectory().getPackage())){
                    imports.add(item.getTypeName());
                }
            });
        }
        return imports;
    }

}
